package cake.srcprojector.parser;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import cake.srcprojector.AbstractJSONWriter;
import cake.srcprojector.DocParser;
import cake.srcprojector.DocParserContext;
import cake.srcprojector.model.Page;
import cake.srcprojector.service.PageService;


public class TOCDocParser extends AbstractJSONWriter implements DocParser {

	
	private PageService pageService = new PageService();
	
	
	public TOCDocParser(OutputStream outputStream) throws IOException {
		super(outputStream);
	}
	

	@Override
	public void startFolder(File folder, DocParserContext docParserContext) throws IOException {
		//write("{\"type\": \"Text\", \"label\": \"" +  folder.getName() + "\", \"expanded\": true, \"children\": [");
		
		Page page = pageService.parsePage(new File(folder, docParserContext.getPageFileName()));
		
		write("{");
		writeAttribute("pageid", page.getPageId());
		write(",");
		writeAttribute("type", "text");
		write(",");
		writeAttribute("label", page.getTitle());
		write(",");
		writeAttribute("expanded", false);
		write(",");
		write("\"children\":[");
	}


	@Override
	public void endFolder(File folder, boolean isLast) throws IOException {
		write("]}");
		
		if (!isLast) {
			write(",");
		}
		
		flush();
	}


	@Override
	public void parsePage(File page, File[] subFolderArray, DocParserContext docParserContext) throws IOException {
		
	}


}
